<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>课程添加</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="../../layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="../../css/public.css" media="all" />
</head>
<body class="childrenBody">
<form class="layui-form" style="width:50%;">
	<input class="layui-hide" id="id" value="" >
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label">课程名</label>
		<div class="layui-input-block">
			<input type="text" class="layui-input name" lay-verify="required" placeholder="请输入课程名">
		</div>
	</div>
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label">先修课</label>
		<div class="layui-input-block">
			<input type="text" class="layui-input pname" lay-verify="required" placeholder="请输入先修课名">
		</div>
	</div>
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label">学分</label>
		<div class="layui-input-block">
			<input type="text" class="layui-input credit" lay-verify="required|number" placeholder="请输入学分">
		</div>
	</div>
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label">课时</label>
		<div class="layui-input-block">
			<input type="text" class="layui-input classhour" lay-verify="required|number" placeholder="请输入课时">
		</div>
	</div>
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label">上课地点</label>
		<div class="layui-input-block">
			<input type="text" class="layui-input address" lay-verify="required" placeholder="请输入上课地点">
		</div>
	</div>
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label">课程简介</label>
		<div class="layui-input-block">
			<textarea placeholder="请输入课程简介" class="layui-textarea info"></textarea>
		</div>
	</div>
	<input class="layui-hide" id="tNameHide" value="" >
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label">授课教师</label>
		<div class="layui-input-block">
			<select name="tName" class="tName" lay-verify="required">
				<option value="">请选择教师...</option>
			</select>
		</div>
	</div>
	<input class="layui-hide" id="classNameHide" value="" >
	<div class="layui-form-item layui-row layui-col-xs12">
		<label class="layui-form-label">上课班级</label>
		<div class="layui-input-block">
			<select name="className" class="className" lay-verify="required">
				<option value="">请选择班级...</option>
			</select>
		</div>
	</div>
	<div class="layui-form-item layui-row layui-col-xs12">
		<div class="layui-input-block">
			<button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="addCourse">立即添加</button>
			<button type="reset" class="layui-btn layui-btn-sm layui-btn-primary">重置</button>
		</div>
	</div>
</form>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript">
layui.use(['form','layer'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;
    
    function getRootPath(){  
        var curPath=window.document.location.href;  
        var pathName=window.document.location.pathname;  
        var pos=curPath.indexOf(pathName);  
        var localhostPaht=curPath.substring(0,pos);  
        var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);  
        return localhostPaht+projectName;
    }
        
    var path = getRootPath();
    
    InitForm();
    
    form.on("submit(addCourse)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        
        // 实际使用时的提交信息
        $.ajax({
  		  url: path+'/course/submit',
  		  data: {
              id: $("#id").val(),
         	  pname : $(".pname").val(),
              name : $(".name").val(),
              credit : $(".credit").val(),
              classhour : $(".classhour").val(),
              address : $(".address").val(),
              info : $(".info").val(),
              tName : data.field.tName,
              className: data.field.className
          },
  		  dataType: 'json',
  		  type: 'post',
  		  success: function(ret){
  			  if(ret.state=="ok"){
         		 top.layer.msg("课程添加成功！");
                 layer.closeAll("iframe");
                 //刷新父页面
                 parent.location.reload();
  			  } else {
  				  top.layer.msg("课程添加失败！");
  			  }
  		  },
  	  	  error: function(){
  	  		top.layer.msg("课程添加失败！");
  			  }
  	  });
        return false;
    });

    //下拉初始化
    function InitForm(){
    	$.ajax({
    		type: 'post',
    		url: path+'/course/form',
    		dataType: 'json',
    		success: function(responseM){
    			if(responseM.class.length > 0){
    				//加载班级
    				var className = $("#classNameHide").val();
    				for(var i = 0;i < responseM.class.length; i++){
    					var name = responseM.class[i].name;
    					var value = responseM.class[i].id;
    					if(className == name){
    						$(".className").append('<option value="'+value+'" selected="">'+name+'</option>');
    					}else{
	    					$(".className").append('<option value="'+value+'">'+name+'</option>');
    					}
    				}
    				//加载教师
    				var tName = $("#tNameHide").val();
    				for(var i = 0;i < responseM.teacher.length; i++){
    					var name = responseM.teacher[i].name;
    					var value = responseM.teacher[i].id;
    					if(tName == name){
    						$(".tName").append('<option value="'+value+'" selected="">'+name+'</option>');
    					}else{
	    					$(".tName").append('<option value="'+value+'">'+name+'</option>');
    					}
    				}
    				
    				form.render('select');
    			}else{
    				layer.msg("下拉数据加载失败...");
    			}
    		}
    	})
    }
    
});
</script>
</body>
</html>